package UI.LessonPlanViewer
{
	import Events.UIToolbarEvent;
	
	import UI.Buttons.*;
	
	import flash.display.*;
	import flash.events.*;
	
	import mx.core.UIComponent;
	
	public class UILessonPlanViewerToolbar extends UIComponent
	{	
		private var openButton:UIButton;
		private var saveButton:UIButton;
		private var addTaskButton:UIButton;
		private var clearButton:UIButton;
		private var historyButton:UIButton;
		
		public function UILessonPlanViewerToolbar(assets:Object)
		{
			super();
			this.addTaskButton = new UIButton(assets.AddTaskButton);
			this.clearButton = new UIButton(assets.ClearButton);
			this.openButton = new UIButton(assets.OpenButton);
			this.saveButton = new UIButton(assets.SaveButton);
			this.historyButton = new UIButton(assets.HistoryButton);
			
			addEventListeners();
			positionElements();
			addChildren();
		}
		
		private function addEventListeners():void
		{
			addTaskButton.addEventListener(MouseEvent.CLICK, dispatchClickEvent);
			clearButton.addEventListener(MouseEvent.CLICK, dispatchClickEvent);
			openButton.addEventListener(MouseEvent.CLICK, dispatchClickEvent);
			saveButton.addEventListener(MouseEvent.CLICK, dispatchClickEvent);
			historyButton.addEventListener(MouseEvent.CLICK, dispatchClickEvent);
		}
		
		private function dispatchClickEvent(e:MouseEvent):void
		{
			var eventType:String;
			if(e.target==addTaskButton)
				eventType = UIToolbarEvent.ADD_TASK;
			else if(e.target==clearButton)
				eventType = UIToolbarEvent.CLEAR;
			else if(e.target==openButton)
				eventType = UIToolbarEvent.OPEN;
			else if(e.target==saveButton)
				eventType = UIToolbarEvent.SAVE;
			else if(e.target==historyButton)
				eventType = UIToolbarEvent.HISTORY;
			dispatchEvent(new UIToolbarEvent(eventType));
		}
		
		private function positionElements():void
		{
			saveButton.x = openButton.width+openButton.x;
			addTaskButton.x = saveButton.x+saveButton.width;
			clearButton.x = addTaskButton.x+addTaskButton.width;
			historyButton.x = clearButton.x+clearButton.width;
		}
		
		private function addChildren():void
		{
			addChild(openButton);
			addChild(saveButton);
			addChild(addTaskButton);
			addChild(clearButton);
			addChild(historyButton);
		}
		
		public override function set width(w:Number):void
		{
			var overflowWidth:Number = w-(historyButton.x+historyButton.width);
			openButton.x = overflowWidth/2;
			positionElements();
		}
		
		public override function get width():Number
		{
			return openButton.x+historyButton.x+historyButton.width;
		}
		
		public override function get height():Number
		{
			return Math.max(openButton.height,saveButton.height,addTaskButton.height,clearButton.height,historyButton.height);
		}
	}
}